import Vue, { VNodeData, VNode } from "vue";
import DeleteMessageBoxVue from "../components/DeleteMessageBox";
import { relativeTime as rt } from "@/vendors/dayjs";
import { formatDate } from "feok-lib";
import { fmoney, placeholder } from "@/utils/assist.ts";
import { get } from "lodash";
import store from "@/store";

/**
 * 获取枚举
 * @param {String} formName 表单名称
 */
const getEnums: any = (key: any) => {
  const o = store.state.app.enum;
  return Object.keys(o[key] || {}).map((item) => ({
    value: item,
    text: o[key][item],
    label: o[key][item]
  }));
};
const getEnums2: any = (key: any) => {
  const o = require("@/enums/index").default;
  return Object.keys(o[key] || {}).map((item) => ({
    value: item,
    text: o[key][item],
    label: o[key][item]
  }));
};

/**
 * 日期差
 * @param time 时间
 */
const relativeTime: any = (time: string) => time ? rt(time) : "";

class DeleteMessageBox extends Vue {
  public loading = false;
  public del(msg: string, align: string) {
    return new Promise((resolve, reject) => {
      const h = this.$createElement;
      const vnode: any = h(DeleteMessageBoxVue, {
        props: {
          message: msg,
          align
        }
      });
      this.$msgbox({
        message: vnode,
        title: "",
        confirmButtonText: " 确定删除 ",
        confirmButtonClass: "delete-btn-class el-button--danger",
        center: true,
        closeOnClickModal: false,
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "删除中...";
            instance.showClose = false;
            resolve(() => {
              done();
              instance.confirmButtonLoading = false;
            });
          } else {
            done();
          }
        }
      });
    });
  }
}

const enums = (str: string) => {
  return get(store.state.app.enum, str, "");
};

const enums2 = (str: string) => {
  const o = require("@/enums/index").default;
  return get(o, str, "");
};

// 新增全局方法
// Vue.t = enums;
// 新增实例方法
Vue.prototype.$t = enums;
Vue.prototype.$t2 = enums2;
Vue.prototype.$tl = getEnums;
Vue.prototype.$tl2 = getEnums2;
Vue.prototype.$rt = relativeTime;
Vue.prototype.$del = new DeleteMessageBox().del;
Vue.prototype.$fd = formatDate;
Vue.prototype.$fmoney = fmoney;
Vue.prototype.$ph = placeholder;


export default {
  getEnums,
  getEnums2
};
